package com.chuantou.yuba.ui.fragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.chuantou.yuba.R;
import com.chuantou.yuba.adapter.FishingAdapter;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.BaseGsonRequest;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.FishingDataTO;
import com.chuantou.yuba.api.to.FishingTO;
import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.ui.FishingDetailActivity;
import com.chuantou.yuba.ui.widget.LoadingView.Style;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.Constants;

/**
 * 首页->附近约钓fragment
 * 
 * @author dl1001
 * 
 */
public class NearFishingFragment extends PaginationListFragment<FishingDataTO> {
	private boolean mIsLogin;
	private FishingAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext.registerReceiver(mFishingChangedReceiver, new IntentFilter(Constants.INTENT_ACTION_SEND_FISHING));
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View contentView = super.onCreateView(inflater, container, savedInstanceState);
		if (mAdapter == null) {
			mAdapter = new FishingAdapter(getActivity());
			mListView.setAdapter(mAdapter);
			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					Object obj = parent.getItemAtPosition(position);
					if (obj != null && obj instanceof FishingTO) {
						FishingTO fishingTO = (FishingTO) obj;
						Intent intent = new Intent(getActivity(), FishingDetailActivity.class);
						intent.putExtra(Constants.EXTRA_DATA, fishingTO);
						startActivity(intent);
					}
				}
			});
			startRequest();
		}
		return contentView;
	}

	@Override
	protected BaseGsonRequest<FishingDataTO> getRequest(Listener<FishingDataTO> listener, ErrorListener errorListener) {
		String url = UriHelper.getFishingListUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_PAGE_SIZE, String.valueOf(Constants.DEFAULT_PAGE_SIZE));
		params.put(UriHelper.PARAMS_PAGE_NO, String.valueOf(1));
		AccountManager accountManager = AccountManager.getInstance(mContext);
		mIsLogin = accountManager.isLogined();
		if (mIsLogin) {
			if (params.containsKey(UriHelper.PARAMS_STATE_ID)) {
				UserTO user = accountManager.getUserTO();
				params.put(UriHelper.PARAMS_STATE_ID, user.getStateId());
				params.put(UriHelper.PARAMS_CITY_ID, user.getCityId());
			}
		}
		GsonRequest<FishingDataTO> request = new GsonRequest<FishingDataTO>(Method.POST, url, listener, errorListener, params, FishingDataTO.class);
		return request;
	}

	private BroadcastReceiver mFishingChangedReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			mListView.setSelection(0);
			load(1);
		}
	};

	public void onDestroy() {
		mContext.unregisterReceiver(mFishingChangedReceiver);
		super.onDestroy();
	};

	@Override
	public void onResponse(FishingDataTO response) {
		if (getActivity() != null) {
			super.onResponse(response);
			if (response != null) {
				List<FishingTO> list = response.getFishingList();
				// 第一页
				if (response.getCurrentPage() <= 1) {
					mAdapter.clear();
				}
				mAdapter.add(list);
			}
			if (mAdapter.getCount() <= 0) {
				mLoadingView.setStyle(Style.EMPTY, getEmptyString());
			}
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		Map<String, String> params = mRequest.getParams();
		AccountManager accountManager = AccountManager.getInstance(mContext);
		boolean isLogin = accountManager.isLogined();
		if (isLogin) {
			if (params.containsKey(UriHelper.PARAMS_STATE_ID)) {
				UserTO user = accountManager.getUserTO();
				params.put(UriHelper.PARAMS_STATE_ID, user.getStateId());
				params.put(UriHelper.PARAMS_CITY_ID, user.getCityId());
			}
		} else {
			params.remove(UriHelper.PARAMS_STATE_ID);
			params.remove(UriHelper.PARAMS_CITY_ID);
		}
		// 如果两次登录状态不同，则重新加载数据
		if (isLogin != mIsLogin) {
			load(1);
		}
	}

	@Override
	protected String getEmptyString() {
		return mContext.getString(R.string.no_fishing_content);
	}

}
